<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="userAccount"
          name="账户"
          label="账户"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
          v-model="userPassword"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
      <van-button round block type="success" @click="goRegister">
        注册
      </van-button>
    </div>
  </van-form>

</template>
<script setup>
import {ref} from "vue";
import myAxios from "../plugins/myAxios.ts";
import {showSuccessToast} from "vant";
import {useRoute, useRouter} from "vue-router";

const router = useRouter()
const route = useRoute()
const userAccount = ref('');
const userPassword = ref('');
const onSubmit = async () => {
  const res = await myAxios.post('/userInfo/login', {
    userAccount: userAccount.value,
    userPassword: userPassword.value
  })
  console.log(res.data)
  if (res.code === 0 && res.data != null) {
    const redirectUrl = route.query?.redirect ?? '/'
    showSuccessToast('登陆成功')
    window.location.href = redirectUrl
  }
}
const goRegister = () => {
  router.push({
    path: '/register'
  })
}
</script>
<style scoped>

</style>